using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using iFinity.DNN.Modules.CacheMaster.Entities;

namespace iFinity.DNN.Modules.CacheMaster
{
	public partial class CacheView : PortalModuleBase
    {
        #region controls
        protected Label lblCacheItem;
        protected Repeater rptCacheItems;
        private CacheItemInfoCol _cacheItems;

        private int _timezoneOffset = 0;

        #endregion
        #region Web Form Designer generated code
        [System.Diagnostics.DebuggerStepThrough]
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
            this.rptCacheItems.ItemDataBound += new RepeaterItemEventHandler(rptCacheItems_ItemDataBound);
        }

        #endregion
        private void Page_Load(object sender, System.EventArgs e)
		{

            try
            {
                if (!Page.IsPostBack)
                {
                    _timezoneOffset = this.PortalSettings.TimeZoneOffset;
                    _timezoneOffset += this.UserInfo.Profile.TimeZone;
                    _cacheItems = CacheController.GetCacheItems(this.PortalId);
                    if (_cacheItems.Count > 0)
                    {
                        string itemCount = UIController.GetResourceString(LocalResourceFile, "CacheItemCount.Text", "Found {0} items at time of {1}");
                        lblCacheItem.Text = string.Format(itemCount, _cacheItems.Count, UIController.FormatUTCDate(_timezoneOffset, DateTime.UtcNow));
                        rptCacheItems.DataSource = _cacheItems;
                        rptCacheItems.DataBind();

                    }
                    else
                    {
                        lblCacheItem.Text = Localization.GetString("NoItems.Text", LocalResourceFile);
                    }
                }
                //require jQuery registration
                DotNetNuke.Framework.jQuery.RequestRegistration();
                
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
		}
        protected void rptCacheItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Header:
                    HtmlGenericControl cacheItemKeyHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemKeyHdr");
                    HtmlGenericControl cacheItemTypeHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemTypeHdr");
                    HtmlGenericControl cacheItemCreatedDateHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemCreatedDateHdr");
                    HtmlGenericControl cacheItemExpiryDateHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemExpiryDateHdr");
                    HtmlGenericControl cacheItemSlidingExpirationHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemSlidingExpirationHdr");
                    HtmlGenericControl cacheItemBinSerializableHdr =
                        (HtmlGenericControl)e.Item.FindControl("cacheItemBinSerializableHdr");
                    HtmlGenericControl cacheItemCountHdr = (HtmlGenericControl) e.Item.FindControl("cacheItemCountHdr");
                    cacheItemKeyHdr.InnerText = Localization.GetString("CacheItemKeyHdr.Text", LocalResourceFile);
                    cacheItemBinSerializableHdr.InnerText = Localization.GetString("CacheItemBinSerializableHdr.Text", LocalResourceFile);
                    cacheItemTypeHdr.InnerText = Localization.GetString("CacheItemTypeHdr.Text", LocalResourceFile);
                    cacheItemExpiryDateHdr.InnerText = Localization.GetString("CacheItemExpiryDateHdr.Text", LocalResourceFile);
                    cacheItemCreatedDateHdr.InnerText = Localization.GetString("CacheItemCreatedDateHdr.Text", LocalResourceFile);
                    cacheItemSlidingExpirationHdr.InnerText = Localization.GetString("CacheItemSlidingExpirationHdr.Text", LocalResourceFile);
                    cacheItemCountHdr.InnerText = Localization.GetString("CacheItemCountHdr.Text", LocalResourceFile);
                    break;

                case ListItemType.Item:
                case ListItemType.AlternatingItem:
            
                    CacheItemInfo cacheItem = (CacheItemInfo)e.Item.DataItem;
                    HtmlGenericControl cacheItemKey = (HtmlGenericControl)e.Item.FindControl("cacheItemKey");
                    HtmlGenericControl cacheItemBinSerializable = (HtmlGenericControl)e.Item.FindControl("cacheItemBinSerializable");
                    HtmlGenericControl cacheItemType = (HtmlGenericControl)e.Item.FindControl("cacheItemType");
                    HtmlGenericControl cacheItemCreatedDate = (HtmlGenericControl)e.Item.FindControl("cacheItemCreatedDate");
                    HtmlGenericControl cacheItemExpiryDate = (HtmlGenericControl)e.Item.FindControl("cacheItemExpiryDate");
                    HtmlGenericControl cacheItemSlidingExpiration = (HtmlGenericControl)e.Item.FindControl("cacheItemSlidingExpiration");
                    HtmlGenericControl cacheItemCount = (HtmlGenericControl)e.Item.FindControl("cacheItemCount");
                    HtmlAnchor cacheItemLink = (HtmlAnchor)e.Item.FindControl("cacheItemLink");
                    cacheItemLink.HRef = UIController.GetCacheItemUrl(this.TabId, this.ModuleId, cacheItem.CacheKey);
                    
                    bool serializable = cacheItem.BinarySerializable && cacheItem.BinaryDeSerializable;
                    if (serializable == false)
                        cacheItemBinSerializable.Attributes.Add("class", "cacheItemError");
                    cacheItemBinSerializable.InnerText = serializable.ToString();
                    cacheItemKey.InnerText = cacheItem.CacheKey;
                    cacheItemType.InnerText = cacheItem.CacheObjectType;
                    cacheItemExpiryDate.InnerText = UIController.FormatUTCDate(_timezoneOffset, cacheItem.UtcExpiryDate);
                    cacheItemCreatedDate.InnerText = UIController.FormatUTCDate(_timezoneOffset, cacheItem.UtcCreatedDate);
                    if (cacheItem.Count > -1)
                        cacheItemCount.InnerText = cacheItem.Count.ToString();
                    else
                        cacheItemCount.InnerText = "Not a collection";

                    if (cacheItem.SlidingExpiration.TotalSeconds > 0)
                        cacheItemSlidingExpiration.InnerText = cacheItem.SlidingExpiration.ToString();
                    else
                        cacheItemSlidingExpiration.InnerText = "No sliding expiration";
                    
                    break;
            }
        }

       
	}
}